package com.hksj.base.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.base.mapper.DefaultAlarmMapper;
import com.hksj.base.query.AlarmNumOverviewQuery;
import com.hksj.base.query.AlarmQuery;
import com.hksj.base.query.AlarmRankQuery;
import com.hksj.base.query.BaseSpecialityQuery;
import com.hksj.base.vo.AlarmNumOverviewVO;
import com.hksj.base.vo.AlarmRankVO;
import com.hksj.base.vo.AlarmRecordVO;
import com.hksj.common.core.common.SpecialityModelCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class DefaultAlarmService {
    @Autowired
    private DefaultAlarmMapper defaultAlarmMapper;
    @Autowired
    private DefaultDeviceService defaultDeviceService;

    public Page<AlarmRecordVO> pageAlarmHistory(AlarmQuery query) {
        Page<AlarmRecordVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        List<AlarmRecordVO> list = defaultAlarmMapper.pageAlarmHistory(query,query.getDisAblePage() ? null : page);
        defaultDeviceService.wrapAddr(list);

        if (!ObjectUtils.isEmpty(list)){
            for (AlarmRecordVO alarmRecordVO : list) {
                if (Objects.equals(alarmRecordVO.getProperty(),SpecialityModelCodeEnum.Jfaq_lous.getCode())){
                    alarmRecordVO.setValue(Objects.equals("1",alarmRecordVO.getValue()) ? "是" : "否");
                }
//                alarmRecordVO.setPropertyName(PropertyCodeUnitEnum.getName(alarmRecordVO.getProperty()));
            }
        }

        page.setRecords(list);
        return page;
    }

    public List<AlarmRankVO> listDeviceAlarmRank(AlarmRankQuery query) {
        List<AlarmRankVO> list = defaultAlarmMapper.listDeviceAlarmRank(query);
        defaultDeviceService.wrapAddr(list);
        return list;
    }

    public List<AlarmRecordVO> listCurrentAlarm(BaseSpecialityQuery query){
        List<AlarmRecordVO> list = defaultAlarmMapper.listCurrentAlarm(query);
        defaultDeviceService.wrapAddr(list);
        return list;
    }

    public AlarmNumOverviewVO getAlarmNumOverview(AlarmNumOverviewQuery query) {
        Date now = new Date();
        AlarmNumOverviewVO res = new AlarmNumOverviewVO();
        //本日
        String startTime = DateUtil.formatDateTime(DateUtil.beginOfDay(now));
        String endTime = DateUtil.formatDateTime(DateUtil.endOfDay(now));
        query.setStartTime(startTime);
        query.setEndTime(endTime);
        Integer num = defaultAlarmMapper.getAlarmNum(query);
        res.setDayAlarmNum(num);

        //本周
        startTime = DateUtil.formatDateTime(DateUtil.beginOfWeek(now));
        endTime = DateUtil.formatDateTime(DateUtil.endOfWeek(now));
        query.setStartTime(startTime);
        query.setEndTime(endTime);
        num = defaultAlarmMapper.getAlarmNum(query);
        res.setWeekAlarmNum(num);

        //本月
        startTime = DateUtil.formatDateTime(DateUtil.beginOfMonth(now));
        endTime = DateUtil.formatDateTime(DateUtil.endOfMonth(now));
        query.setStartTime(startTime);
        query.setEndTime(endTime);
        num = defaultAlarmMapper.getAlarmNum(query);
        res.setMonthAlarmNum(num);
        return res;
    }
}
